﻿using UnityEngine;
#if UNITY_IOS && !UNITY_EDITOR
using System.Runtime.InteropServices;
#endif

namespace BToolkit
{
    public class AppInfo
    {

        /// <summary>
        /// 仅在Unity主线程可取
        /// </summary>
        public static string GetAppPackageName()
        {
#if UNITY_EDITOR && UNITY_ANDROID
            return UnityEditor.PlayerSettings.GetApplicationIdentifier(UnityEditor.BuildTargetGroup.Android);
#elif UNITY_EDITOR && UNITY_IOS
            return UnityEditor.PlayerSettings.GetApplicationIdentifier(UnityEditor.BuildTargetGroup.iOS);
#elif UNITY_ANDROID
            AndroidJavaClass jc = new AndroidJavaClass("cn.helper.AppInfo");
            return jc.CallStatic<string>("getAppPackageName");
#elif UNITY_IOS
            return _GetAppPackageName();
#else
            return "";
#endif
        }

        /// <summary>
        /// 仅在Unity主线程可取
        /// </summary>
        public static string GetAppVersionName()
        {
#if UNITY_EDITOR
            return UnityEditor.PlayerSettings.bundleVersion;
#elif UNITY_ANDROID
            AndroidJavaClass jc = new AndroidJavaClass("cn.helper.AppInfo");
            return jc.CallStatic<string>("getAppVersionName");
#elif UNITY_IOS
            return _GetAppVersionName();
#else
            return "";
#endif
        }

        /// <summary>
        /// 仅在Unity主线程可取
        /// </summary>
        public static int GetAppVersionCode()
        {
#if UNITY_EDITOR && UNITY_ANDROID
            return UnityEditor.PlayerSettings.Android.bundleVersionCode;
#elif UNITY_EDITOR && UNITY_IOS
            return int.Parse(UnityEditor.PlayerSettings.iOS.buildNumber);
#elif UNITY_ANDROID
            AndroidJavaClass jc = new AndroidJavaClass("cn.helper.AppInfo");
            return jc.CallStatic<int>("getAppVersionCode");
#elif UNITY_IOS
            return _GetAppVersionCode();
#else
            return 0;
#endif
        }

#if UNITY_IOS && !UNITY_EDITOR
        [DllImport("__Internal")]
        static extern string _GetAppPackageName();

        [DllImport("__Internal")]
        static extern string _GetAppVersionName();

        [DllImport("__Internal")]
        static extern int _GetAppVersionCode();
#endif
    }
}